package com.example.user.customviewdemo.slideletelistview.scroller;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * Created by user on 2016/5/24.
 */
public class MyScrollerListView extends ListView {


    private MyScrollerLinearLayout myScrollerLinearLayout;
    private OnItemScrollListener mOnItemScrollListener;

    /**
     * 声明一个一个OnMyScrollListener接口
     * 用于将滑动的item传出去
     */
    public interface OnItemScrollListener{
        /**
         *
         * @param myScrollerLinearLayout 滑动展开的LinearLayout
         */
        void onScroll(MyScrollerLinearLayout myScrollerLinearLayout);
    }

    public MyScrollerListView(Context context) {
        super(context);
    }

    public MyScrollerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollerListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int action = ev.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                int x = (int) ev.getX();
                int y = (int) ev.getY();
                int position = pointToPosition(x,y);
                MyScrollerListViewActivity.MyScrollerListViewAdapter.DataHolder dataHolder = (MyScrollerListViewActivity.MyScrollerListViewAdapter.DataHolder) getItemAtPosition(position);
                myScrollerLinearLayout = dataHolder.rootLayout;
               /* if(mOnItemScrollListener != null){
                    mOnItemScrollListener.onScroll(dataHolder.rootLayout);
                }*/
                break;
        }

        if(myScrollerLinearLayout != null){
            myScrollerLinearLayout.dispatchTouchEvent(ev);
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 设置item滑动的监听
     * @param onItemScrollListener
     */
    public void setOnItemScrollListener(OnItemScrollListener onItemScrollListener){
        this.mOnItemScrollListener = onItemScrollListener;
    }
}
